﻿--[[
   @brief    model for friend list ui
   @author   vincentlao
   @version  1.0
   @date     2015-07-09
--]]

friend_list_model =
{
    mini_mode_loaded = false;
    is_get_social_test_info_ = false;
    is_get_friend_list_done = false;
    social_user_level = 0;
    social_invite_code_count = 0;

    --
    friend_search_menu_reddot = "false";

    --三种状体颜色
    TGP_ONLINE_COLOR = "0x00C34E";

    TGP_OFFLINE_COLOR = "0x65615C";

    OFFLINE_NAME_COLOR = "0xB3B3B3";
    ONLINE_NAME_COLOR = "0x000000";
    
    GAME_ONLINE_COLOR = "0xB3B3B3";
    GAME_ONLINE_COLOR_SEL = "0x009CF6";

    data_refreshed_ = nil;

    ierd_tgp.tpf_ui.Require("social\\friend_message\\model.lua");
    --friend在线状态说明:
    --0:tgp在线， 1 tgp离线
    TGP_ONLINE_STATUS = 0;
    TGP_OFFLINE_STATUS = 1;
    TGP_GAMEING_STATUS= 2;

    friend_table_ = {};
    show_title_data_new_msg_ = true;
    has_get_unread_msg_count = false;

    friend_sort_func = function(a, b)
        local a_online_status = 0;
        local b_online_status = 0;

        if a.status ~= nil then
            a_online_status = a.status;
        end

        if b.status ~= nil then
            b_online_status = b.status;
        end

        local a_game_id = 0;
        local b_game_id = 0;

        if a_online_status == 0 then
            local cur_status, game_id = friend_list_model.get_friend_status(a.friend_id);
            if game_id > 0 then
                a_game_id = game_id;
                a_online_status = -1;
            end   
        end

        if b_online_status == 0 then
            local cur_status, game_id = friend_list_model.get_friend_status(b.friend_id);
            if game_id > 0 then
                b_game_id = game_id;
                b_online_status = -1;
            end
        end

        if a_online_status ~= b_online_status then
            return a_online_status < b_online_status;
        end

        local a_tgp_level = 0;
        local b_tgp_level = 0;

        a_tgp_level = a.level;
        b_tgp_level = b.level;
        if a_tgp_level ~= b_tgp_level then
            return a_tgp_level > b_tgp_level;
        end

        if a_game_id > 0 and b_game_id > 0 and a_game_id ~= b_game_id then
            return a_game_id > b_game_id;
        end

        local a_last_msg_time = 0;
        local b_last_msg_time = 0;

        local a_message_count = 0;
        local b_message_count = 0;

        if a.history_unread_msg_count then
            a_message_count = a.history_unread_msg_count;
        end

        a_message_count = a_message_count;

        if b.history_unread_msg_count then
            b_message_count = b.history_unread_msg_count;
        end

        b_message_count = b_message_count;

        if a.last_msg_ and a_message_count > 0 then
            a_last_msg_time = a.last_msg_.time;
        end

        if b.last_msg_ and b_message_count > 0  then
            b_last_msg_time = b.last_msg_.time;
        end

        if a_last_msg_time ~= b_last_msg_time then
            return a_last_msg_time > b_last_msg_time;
        end

        if a.name ~= b.name then
            return a.name < b.name;
        end

        return a.friend_id < b.friend_id;
    end;

    --[[  
    冒泡排序  
        array 需要排序的数字  
        compareFunc 比较函数  
    --]]  
    bubble_sort = function(array, compareFunc)  
        local len = #array;
        local i = len;

        while i > 0 do  
            j=1;
            while j< len do
                if compareFunc(array[j],array[j+1]) then
                    array[j],array[j+1] = array[j+1],array[j];
                end  
                j = j + 1;  
            end  
            i = i - 1;
        end  
    end;

    init = function()
        ierd_tgp.tpf_ui.Log("[friend_list_model]init");
        friend_list_model.friend_table_ = {};
        friend_list_model.data_refreshed_ = nil;
        if base_utility.is_type_of_oversea_ver() then
            friend_list_model.GAME_ONLINE_COLOR_SEL = "0xFFB219";
        end
        comm_center.listen_broadcast('i_service_get_friend_unread_msg_num', friend_list_model.on_service_get_friend_unread_msg_num); -- 来自cross
    end;

    uninit = function()
        ierd_tgp.tpf_ui.Log("[friend_list_model]uninit");
    end;

    sort = function()
        table.sort( friend_list_model.friend_table_, friend_list_model.friend_sort_func);
        friend_list_model.data_refreshed_ = nil;
        --friend_list_model.bubble_sort(friend_list_model.friend_table_, friend_list_model.friend_sort_func);
    end;

    get_friend_list = function()
        return friend_list_model.friend_table_;
    end;

    update_data_from_social_info_center = function ()
        for index = 1, #friend_list_model.friend_table_ do
            input = {list_id={friend_list_model.friend_table_[index]['friend_id'],}};
            output = comm_center.call_service('i_service_social_info_center_get_user_info', input);
            local friend_info = output['list_user_info'][1];
            base_utility.log("[friend_list_model][get_friend_info] friend_id:"..tostring(friend_info.friend_id)..", name:"..tostring(friend_info.name)..", icon:"..tostring(friend_info["url"]));
            -- 加保护，避免出现数据中心没有数据覆盖现有数据
            -- 首次加载好友列表get_friendlist_done时，name 和 facue_url 是可以获取到的
            if string.len(output['list_user_info'][1]['name']) > 0 then
                friend_list_model.friend_table_[index]['name'] = output['list_user_info'][1]['name'];
            end

            friend_list_model.friend_table_[index]['status'] = output['list_user_info'][1]['status'];

            if string.len(output['list_user_info'][1]['url']) > 0 then
                friend_list_model.friend_table_[index]['url'] = output['list_user_info'][1]['url'];
                -- 提前下载头像
                base_utility.download_files(output['list_user_info'][1]['url']);
            end
            friend_list_model.data_refreshed_ = nil;
        end;
    end;
    
    set_friendlist_to_social_info_center = function ()
        -- 功能已经移动到lol_game_status_mgr 模块去 管理需要请求游戏状态的好友列表
        --[[tb_outputlist = {};
        data = {};

        for index = 1, #friend_list_model.friend_table_ do
            table.insert(tb_outputlist, friend_list_model.friend_table_[index].friend_id);
        end

        data['friend_list'] = tb_outputlist;
        comm_center.call_service("i_service_social_info_center_set_lol_gaming_friend_list", data);]]
    end;

    set_friend_userinfo_to_social_info_center = function (friend_list)
        data = {};
        data['friend_list'] = friend_list;
        comm_center.call_service("i_service_social_info_center_set_friend_user_info", data);
    end;

    set_friend_list = function(friend_list)
       friend_list_model.set_friend_userinfo_to_social_info_center(friend_list);
       friend_list_model.friend_table_ = friend_list;
       friend_list_model.data_refreshed_ = nil;
       friend_list_model.update_data_from_social_info_center();
       friend_list_model.set_friendlist_to_social_info_center();

       event_center.send_event(social_event.SOCIAL_FRIEND_COUNT_CHANGE, "{}");
    end;

    add_friend_info = function(pos, friend_info)
      friend_list_model.set_friend_userinfo_to_social_info_center({friend_info,});
      if friend_list_model.friend_table_ == nil then
          friend_list_model.friend_table_ = {};
      end

       table.insert(friend_list_model.friend_table_, pos, friend_info);
       friend_list_model.update_data_from_social_info_center();
       friend_list_model.set_friendlist_to_social_info_center();
       friend_list_model.data_refreshed_ = nil;
       event_center.send_event(social_event.SOCIAL_FRIEND_COUNT_CHANGE, "{}");
    end;
    
    remove_friend_info = function(friend_info)
       for index = 1, #friend_list_model.friend_table_ do
          if friend_info.friend_id == friend_list_model.friend_table_[index].friend_id then
            table.remove(friend_list_model.friend_table_, index);
            friend_list_model.data_refreshed_ = nil;
            break;
          end
       end
       friend_list_model.set_friendlist_to_social_info_center();
       friend_list_model.update_title_ui();
       event_center.send_event(social_event.SOCIAL_FRIEND_COUNT_CHANGE, "{}");
    end;

    remove_friend_info_by_id = function(friend_id)
       for index = 1, #friend_list_model.friend_table_ do
          if friend_id == friend_list_model.friend_table_[index].friend_id then
            table.remove(friend_list_model.friend_table_, index);
            friend_list_model.data_refreshed_ = nil;
            break;
          end
       end
       friend_list_model.set_friendlist_to_social_info_center();
       friend_list_model.update_title_ui();
       event_center.send_event(social_event.SOCIAL_FRIEND_COUNT_CHANGE, "{}");
    end;

    update_name = function (friend_id, friend_name, status, url)
      for index = 1, #friend_list_model.friend_table_ do
          if friend_id == friend_list_model.friend_table_[index].friend_id then
            friend_list_model.friend_table_[index]['name'] = friend_name
            friend_list_model.friend_table_[index]['status'] = status
            friend_list_model.friend_table_[index]['url'] = url;
            local friend_info = friend_list_model.friend_table_[index];
            base_utility.log("[friend_list_model][update_name] friend_id:"..tostring(friend_info.friend_id)..", name:"..tostring(friend_info.name)..", icon:"..tostring(friend_info["url"]), true);
            friend_list_model.data_refreshed_ = nil;
            break;
          end
       end
       
        friend_list_model.update_title_ui();
    end;

    update_remark = function (friend_id, new_remark)
       if nil == friend_list_model.friend_table_ then
            return false;
       end
       for index = 1, #friend_list_model.friend_table_ do
          if friend_id == friend_list_model.friend_table_[index].friend_id then
              friend_list_model.friend_table_[index]['remark'] = new_remark;
              base_utility.log("[friend_list_model][update_remark] friend_id:"..
                                tostring(friend_id)..", remark " .. new_remark);
              friend_list_model.data_refreshed_ = nil;
              return true;
          end
       end
       return false;
    end;

    is_in_friend_list = function (tb_inputlist)
        tb_outputlist = {};
        for i = 1, #tb_inputlist do
            friend_id = tb_inputlist[i];
            for index = 1, #friend_list_model.friend_table_ do
                if friend_id == friend_list_model.friend_table_[index].friend_id then
                    table.insert(tb_outputlist, friend_id);
                end;
            end;
        end;

        return tb_outputlist
    end;

    get_friend_info = function(friend_id)
      for index = 1, #friend_list_model.friend_table_ do
          if friend_id == friend_list_model.friend_table_[index].friend_id then
            friend_info = friend_list_model.friend_table_[index];
            base_utility.log("[friend_list_model][get_friend_info] friend_id:"..tostring(friend_info.friend_id)..", name:"..tostring(friend_info.name)..", icon:"..tostring(friend_info["url"]));
            return friend_list_model.friend_table_[index];
          end
      end 
      return nil;
    end;

    get_friend_info_by_index = function(index)
      return friend_list_model.friend_table_[index];
    end;

    get_friend_count = function()
      return #friend_list_model.friend_table_;
    end;

    get_total_unread_msg_count = function()
        local total = 0;
        for index = 1, #friend_list_model.friend_table_ do
            if friend_list_model.friend_table_[index].history_unread_msg_count ~= nil then
                total = total + friend_list_model.friend_table_[index].history_unread_msg_count;
            end
        end

        return total;
    end;

    update_title_ui = function ()
        icount = friend_list_model.get_total_unread_msg_count();
        local data = {};
        if friend_list_model.show_title_data_new_msg_ == true then
            group_ret = comm_center.call_service('i_service_social_group_get_total_unread_msg_count', {});
            data['unread_msg_count'] = icount + group_ret.unread_msg_count;
        else
            data['unread_msg_count'] = 0;
        end
        data['online_user_count'] = friend_list_model.get_total_online_count();
        local out = comm_center.call_service('i_service_msg_box_get_unread_msg_count', {});
        if nil ~= out then
            data['unread_msgbox_count'] = out.count; 
        else
            data['unread_msgbox_count'] = 0;
        end
        -- 废弃，使用 social_title_msg_tips_mgr 模块更新大界面头部信息
        --comm_center.send_broadcast('i_broadcast_social_network_update_title_data', data);
    end;

    try_change_show_title_data_new_msg = function ()
        data = comm_center.call_service('i_service_social_mainwnd_is_shown', {});
        if data.is_show == 1 then
            --friend_list_model.show_title_data_new_msg_ = false;
        else
            --有新消息的时候，如果主面板是隐藏状态，才会重新show_title_data_new_msg_
            friend_list_model.show_title_data_new_msg_ = true;
        end
    end;

    on_group_new_msg = function ()
        friend_list_model.try_change_show_title_data_new_msg();
        friend_list_model.update_title_ui();
    end;

    set_history_unread_msg_count = function(friend_id, count)
        for index = 1, #friend_list_model.friend_table_ do
            if friend_id == friend_list_model.friend_table_[index].friend_id then
                friend_list_model.friend_table_[index].history_unread_msg_count = count;
                friend_list_model.data_refreshed_ = nil;
                break;
            end
        end
        friend_list_model.try_change_show_title_data_new_msg();
        friend_list_model.update_title_ui();
    end;

    clear_history_unread_msg_count = function(friend_id)
        for index = 1, #friend_list_model.friend_table_ do
            if friend_id == friend_list_model.friend_table_[index].friend_id then
                friend_list_model.friend_table_[index].history_unread_msg_count = 0;
                friend_list_model.data_refreshed_ = nil;
                break;
            end
        end

        friend_list_model.update_title_ui();
    end;

    get_history_unread_msg_count = function(friend_id)
        local friend = friend_list_model.get_friend_info(friend_id);
        if friend == nil or friend.history_unread_msg_count == nil then
            return 0;
        else
            return friend.history_unread_msg_count;
        end
    end;  

    set_history_unread_msg_count_remotely = function(friend_id, count)
        for index = 1, #friend_list_model.friend_table_ do
            if friend_id == friend_list_model.friend_table_[index].friend_id then
                friend_list_model.friend_table_[index].history_unread_msg_count_remotely = count;
                friend_list_model.data_refreshed_ = nil;
                break;
            end
        end
    end;


    clear_history_unread_msg_count_remotely = function(friend_id)
        for index = 1, #friend_list_model.friend_table_ do
            if friend_id == friend_list_model.friend_table_[index].friend_id then
                friend_list_model.friend_table_[index].history_unread_msg_count_remotely = 0;
                friend_list_model.data_refreshed_ = nil;
                break;
            end
        end    
    end;

    get_history_unread_msg_count_remotely = function(friend_id)
        local friend = friend_list_model.get_friend_info(friend_id);
        if friend == nil or friend.history_unread_msg_count_remotely == nil then
            return 0;
        else
            return friend.history_unread_msg_count_remotely;
        end
    end;  

    set_last_msg = function(friend_id, msg)
        for index = 1, #friend_list_model.friend_table_ do
          if friend_id == friend_list_model.friend_table_[index].friend_id then
              friend_list_model.friend_table_[index].last_msg_ = msg;
              friend_list_model.data_refreshed_ = nil;
              break;
          end
        end
    end;

    get_last_msg = function(friend_id)
        for index = 1, #friend_list_model.friend_table_ do
          if friend_id == friend_list_model.friend_table_[index].friend_id then
              return friend_list_model.friend_table_[index].last_msg_;
          end
        end
    end;
    
    get_total_online_count = function()
        local total_online = 0;
        for index = 1, #friend_list_model.friend_table_ do
            if friend_list_model.friend_table_[index].status ~= nil then
                local status = friend_list_model.friend_table_[index].status;
                if status == 0 then
                    total_online = total_online + 1;
                end
            end
        end

        return total_online;
    end;

    on_service_get_friend_unread_msg_num = function(data_in)
        local data_out = {};
        data_out.ret = 0;
        data_out.unread_msg_num_list = {};

        for index = 1, #data_in.friend_id_list do
            local tgp_id = data_in.friend_id_list[index];
            local item = {};
            item.szId = tgp_id;
            item.dwNumber = friend_list_model.get_history_unread_msg_count(tgp_id);
            table.insert(data_out.unread_msg_num_list, 1, item);
        end

        comm_center.send_broadcast('i_broadcast_im_on_friend_unread_msg_num', data_out);
    end;

    -- status: 0 在线， 1 离线
    set_friend_online_status = function(friend_id, gameid, status, sub_status)
        local friend_info = friend_list_model.get_friend_info(friend_id);
        local game_id_num = tonumber(gameid);

        if friend_info ~= nil then
            if game_id_num == 55555 then
                --friend_info.status = status; 不用赋值了，用数据中心的
                if status == 1 then
                    friend_info.playing_game = "{}";
                end
            else
                local playing_game = nil;
                if friend_info.playing_game ~= nil and friend_info.playing_game ~= "" then
                    playing_game = base_utility.json_to_table(friend_info.playing_game);
                end

                if playing_game == nil then
                    playing_game = {};
                end

                if playing_game.data == nil then
                    playing_game.data = {};
                end 

                if playing_game.data.list == nil then
                    playing_game.data.list = {};
                end

                local game = {};
                game.id = game_id_num;
                game.time = os.time();
                game.zone = 0;  --状态变化时没有推送大区，只有id。 默认填写0
                game.status = status;
                game.sub_status = sub_status;

                if status == 0 then
                    local exist = false;
                    if #playing_game.data.list > 0 then
                        for i_1 = 1, #playing_game.data.list do
                            if game_id_num == playing_game.data.list[i_1].id then
                                exist = true;
                                playing_game.data.list[i_1] = game; -- 存在则更新状态
                                break;
                            end
                        end
                    end

                    if exist == false then
                        table.insert(playing_game.data.list, 1, game);
                        if playing_game.data.count == nil then
                            playing_game.data.count = 1 
                        else
                            playing_game.data.count = playing_game.data.count + 1;
                        end
                    end
                else
                    if #playing_game.data.list > 0 then
                        for i_2 = 1, #playing_game.data.list do
                            if playing_game.data.list[i_2] ~= nil and playing_game.data.list[i_2].id ~= nil and game_id_num == playing_game.data.list[i_2].id then
                                table.remove(playing_game.data.list, i_2);
                                if playing_game.data.count >= 1 then
                                   playing_game.data.count = playing_game.data.count - 1;
                                end
                            end
                        end
                    end
                end

                friend_info.playing_game = base_utility.table_to_json(playing_game);
            end
        else
            base_utility.log("[friend_list_model][set_friend_online_status] get freind info error, friend_id:"..tostring(friend_id)..", status:"..tostring(status), true);
        end
    end;

    playing_game_sort = function(a, b)
        return a.time > b.time;
    end;

    -- 获取玩家游戏的状态
    get_friend_game_detail_status = function(friend_id, game_id)
        local friend_info = friend_list_model.get_friend_info(friend_id);
        if friend_info ~= nil then
            if friend_info.playing_game ~= nil and friend_info.playing_game ~= "" then
                local playing_game = base_utility.json_to_table(friend_info.playing_game);

                if playing_game ~= nil and playing_game.data ~= nil and playing_game.data.list ~= nil and #playing_game.data.list > 0 then
                    for i = 1, #playing_game.data.list do
                        if playing_game.data.list[i].id == game_id then
                            local data_out = comm_center.call_service('i_service_is_game_id_in_friend_played_game_list', 
                                                                {friend_id = friend_id, game_id = game_id});
                            if data_out.is_played == false then --游戏被玩家隐藏了
                                return nil, nil
                            else
                                return playing_game.data.list[i].status, playing_game.data.list[i].sub_status;
                            end
                        end
                    end
                end 
            end
        end
        return nil, nil
    end;

    --如果是游戏中，返回当前的游戏id
    get_friend_status = function(friend_id)
        local current_status = friend_list_model.TGP_ONLINE_STATUS;

        local online_status = 0;
        local game_id = 0;

        local friend_info = friend_list_model.get_friend_info(friend_id);
        if friend_info == nil then
            return current_status, game_id;
        end

        if friend_info.status ~= nil then
            online_status = friend_info.status;
        end

        if friend_info.playing_game ~= nil and friend_info.playing_game ~= "" then
            local playing_game = base_utility.json_to_table(friend_info.playing_game);
            if playing_game ~= nil and playing_game.data ~= nil and playing_game.data.list ~= nil and #playing_game.data.list > 0 then
                if #playing_game.data.list > 1 then
                    -- 优先使用最新打开的游戏
                    table.sort(playing_game.data.list, friend_list_model.playing_game_sort);
                end

                for i = 1, #playing_game.data.list do
                    local data_out = comm_center.call_service('i_service_is_game_id_in_friend_played_game_list', 
                                                                {friend_id = friend_id, game_id = playing_game.data.list[i].id});
                    if data_out.is_played == false then --游戏被玩家隐藏了
                        game_id = 0;
                    else
                        game_id = playing_game.data.list[i].id;
                        break;
                    end

                end
                -- 优先返回LOL
                -- for i = 1, #playing_game.data.list do
                --     if playing_game.data.list[i].id == GAME_ID.GAME_ID_LOL then
                --         game_id = GAME_ID.GAME_ID_LOL;
                --         break;
                --     end
                -- end
            end 
        end

        if online_status ~= 0 then
            current_status = friend_list_model.TGP_OFFLINE_STATUS;
        else
            if game_id ~= 0 then
                -- local game_info = banner_model.get_game_base_info(game_id);
                -- if game_info ~= nil then --游戏中
                    current_status = friend_list_model.TGP_GAMEING_STATUS;
                -- else
                --     --TGP在线
                --     current_status = friend_list_model.TGP_ONLINE_STATUS;
                -- end
            else
                --TGP在线
                current_status = friend_list_model.TGP_ONLINE_STATUS;
            end
        end
        return current_status, game_id;
    end;

    set_has_ever_got_msg = function(friend_id, has_ever_got_msg)
        for index = 1, #friend_list_model.friend_table_ do
          if friend_id == friend_list_model.friend_table_[index].friend_id then
              friend_list_model.friend_table_[index].has_ever_got_msg = has_ever_got_msg;
              friend_list_model.data_refreshed_ = nil;
              break;
          end
        end
    end;

    get_has_ever_got_msg = function(friend_id)
        for index = 1, #friend_list_model.friend_table_ do
          if friend_id == friend_list_model.friend_table_[index].friend_id then
              if friend_list_model.friend_table_[index].has_ever_got_msg == nil then
                  return false;
              else
                  return friend_list_model.friend_table_[index].has_ever_got_msg;
              end
          end
        end

        return false;
    end;


    get_last_msg_time = function(friend_id)
        for index = 1, #friend_list_model.friend_table_ do
          if friend_id == friend_list_model.friend_table_[index].friend_id then
              if friend_list_model.friend_table_[index].last_msg_ == nil then
                  return 0;
              else
                  return friend_list_model.friend_table_[index].last_msg_.time;
              end
          end
        end

        return 0;
    end;    

    --------------------------
    save_history_qq_friend_list = function(friend_list, group_info)
        friend_list_model.history_friend_list = friend_list;
        friend_list_model.history_group_info = group_info;
    end; 

    get_history_qq_friend_list = function()
        return friend_list_model.history_friend_list, friend_list_model.history_group_info
    end;

    get_one_history_qq_friend = function(tgp_id)
        if friend_list_model.history_friend_list ~= nil then
            for index, friend in ipairs(friend_list_model.history_friend_list) do
                if friend.friend_id == tgp_id then
                    return friend;
                end
            end
        end
        return nil;
    end;

    save_new_qq_friend_list = function(friend_list, group_info)
        friend_list_model.new_friend_list = friend_list;
        friend_list_model.new_group_info = group_info;
    end;

    get_new_qq_friend_list = function()
        return friend_list_model.new_friend_list, friend_list_model.new_group_info
    end;

    save_new_lol_game_friend_list = function(game_friend_list)
        friend_list_model.new_lol_game_list = game_friend_list;
    end;

    get_new_lol_game_friend_list = function()
        return friend_list_model.new_lol_game_list;
    end;

    save_history_lol_game_friend_list = function(game_friend_list)
        friend_list_model.history_lol_game_list = game_friend_list;
    end;

    get_one_history_lol_game_friend = function(friend_id)
        if friend_list_model.history_lol_game_list ~= nil then
            for index, item in ipairs(friend_list_model.history_lol_game_list) do
                if item.friend_id == friend_id then
                    return item;
                end
            end
        end
        return nil;
    end;

    set_msgbox_reddot = function()
        friend_list_model.update_title_ui();
    end;

    clear_msgbox_reddot = function()
        friend_list_model.update_title_ui();
    end;
};

friend_list_model.init();

-- all the index is zero based

friend_vlist_model =
{
    b_discussion_cllapsed_ = true;--记录讨论组是否收起。
    b_group_cllapsed_ = false;--记录群组分组是否收起。
    cache_friend_info = {};

    virtual_node_id_to_group_info = {};
    current_selected_group_id = "";
    current_selected_group_id = "";

    NODE_TYPE_DISCUSSION_TITLE = 1;
    NODE_TYPE_DISCUSSION_NODE = 2;
    NODE_TYPE_GROUP_TITLE = 3;
    NODE_TYPE_GROUP_NODE = 4;
    NODE_TYPE_NO_DISCUSS_TIPS = 5;
    NODE_TYPE_NO_GROUP_TIPS = 6;

    get_user_level_done = false;
    social_user_level = 0;
    max_mode_msg_list_tips_id = 0;
    max_mode_contact_list_tips_id = 0;
    min_mode_msg_list_tips_id = 0;
    min_mode_contact_list_tips_id = 0;

    get_group_list_done = false;
    mainframe_mode = 0; --默认大界面模式

    already_apply_join_group_ids = {};
    join_group_btn_id_to_group_id = {};

    NODE_HEIGHT = 60;

    insert_apply_join_group_id = function(g_id)
        table.insert(friend_vlist_model.already_apply_join_group_ids, g_id);
    end;
    
    is_apply_join_group_id = function(g_id)
        if #friend_vlist_model.already_apply_join_group_ids == 0 then
            return false;
        end
        for i, id in ipairs(friend_vlist_model.already_apply_join_group_ids) do
            if id == g_id then
                return true;
            end
        end
        return false;
    end;

    get_favor_discussion_count = function()
        if friend_vlist_model.b_discussion_cllapsed_ == true then
            return 0;
        end;
        return group_list_model.get_favor_discussion_count();
    end;

    get_group_count = function()
        if friend_vlist_model.b_group_cllapsed_ == true then
            return 0;
        end;
        return friend_list_model.get_friend_count();
    end;

    get_discussion_info_by_index = function(index)
        return group_list_model.get_favor_discussion_group_info_by_index(index + 1);-- index是1开始的，不是zero based
    end;
    
    get_group_info_by_index = function(index)
        return friend_list_model.get_friend_info_by_index(index + 1);-- index是1开始的，不是zero based
    end;

    init = function()
        ierd_tgp.tpf_ui.Log("[friend_vlist_model]init");
    end;

    uninit = function()
        ierd_tgp.tpf_ui.Log("[friend_vlist_model]uninit");
    end;

    get_item_info = function(index)
        --MessageBox(index);
        local info = {};
        --info['name'] = tostring(index - 0);
        group_info = friend_vlist_model.get_group_info_by_index(index - 0);
        --MessageBox(base_utility.table_to_json(group_info));
        if group_info ~= nil then
            info['group_info'] = group_info;
            --local status, game_id = friend_list_model.get_friend_status(group_info.friend_id);
            --if status == nil then
            --    status = 0;
            --end;
            --info['status'] = status;
            --info['game_id'] = game_id;
            return friend_vlist_model.NODE_TYPE_GROUP_NODE, "social\\friend_node.tpf", info, friend_vlist_model.NODE_HEIGHT;
        end;

    end;

    get_total_item_height = function()
        local height = 0;
        local group_count = friend_vlist_model.get_group_count();
        height = height + friend_vlist_model.NODE_HEIGHT * group_count; --群组节点

        return height;
    end;

    first_visible_item_index = function(scrollbar_pos)
        local height = 0;
        local i = 0;
        local max_node = friend_vlist_model.get_group_count() - 1;
        for i = 0, max_node do
            height = height + friend_vlist_model.NODE_HEIGHT;

            if height > scrollbar_pos then
                return i;
            end;
        end;
        return i;
    end;

    offset_of_first_visible_item_index = function(first_visible_index)
        local offset = 0;
        for i = 0, first_visible_index - 1 do
            offset = offset + friend_vlist_model.NODE_HEIGHT;
        end;
        return offset;
    end;

    clicked_treenode = function()
        friend_vlist_model.b_discussion_cllapsed_ = not friend_vlist_model.b_discussion_cllapsed_;
    end;
    clicked_grouptreenode = function()
        friend_vlist_model.b_group_cllapsed_ = not friend_vlist_model.b_group_cllapsed_;
    end;
};

friend_vlist_model.init();
